package edu.washington.cs.pomdb.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Semaphore;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class TrialReportFrame extends JFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 201202281253L;
	private Semaphore semaphore;
	boolean answer;
	double reward;
	double balance;
	private JPanel contentPane;
	boolean isContinued;
	
	public boolean isContinued(){
		try {
			semaphore.acquire();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return isContinued;
	}

	private void createAndShowGUI(){
		contentPane = (JPanel)this.getContentPane();
		contentPane.setLayout(null);
		contentPane.setBorder(BorderFactory.createEtchedBorder());
		contentPane.setBackground(new Color(204, 204, 204));
		
		
		JLabel resultLabel = new JLabel();
		if(answer)
			resultLabel.setText("You are correct!");
		else
			resultLabel.setText("You are incorrect!");
		JLabel rewardLabel = new JLabel();
		rewardLabel.setText(String.format("Reward from last trial is %.2f", reward));
		
		JLabel balanceLabel = new JLabel();
		balanceLabel.setText(String.format("Your current balance is %.2f", balance));
		
		
		GUI.addComponent(contentPane, resultLabel, 100, 100,400,20);
		GUI.addComponent(contentPane, rewardLabel, 100, 150,400,20);
		GUI.addComponent(contentPane, balanceLabel,100, 200,400,20);

		JButton condButton = new JButton();
		condButton.setText("New Trial");
		condButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){
				cond_actionPerformed(e);
			}
			});
		
		JButton saveButton = new JButton();
		saveButton.setText("Save and Exit");
		saveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){
				save_actionPerformed(e);
			}
			});
		GUI.addComponent(contentPane, condButton, 100, 250, 80, 50);
		GUI.addComponent(contentPane, saveButton, 200, 250, 160, 50);

		this.setTitle("Trial Report");	
		this.pack();
		this.setLocation(new Point(200, 200));
		this.setSize(new Dimension(400, 400));
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.setResizable(true);
		this.setVisible(true);	
		
	}
	
	private void cond_actionPerformed(ActionEvent e){
		isContinued = true;
		semaphore.release();
		this.dispose();
	}
	
	private void save_actionPerformed(ActionEvent e){
		isContinued = false;
		semaphore.release();
		this.dispose();
	}
	
	public TrialReportFrame(boolean answer, double reward, double balance){
		super();
		this.answer = answer;
		this.reward = reward;
		this.balance = balance;
		isContinued  = false;
		semaphore = new Semaphore(0);
		try {
			javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
				public void run() {
					createAndShowGUI();
				}
			});
		} catch (Exception e) { 
			System.err.println("TrialReportFrame.createGUI didn't successfully complete");
			e.printStackTrace();
		}
	}
}
